package cn.ytian.scashier;

import java.io.*;

import static cn.ytian.scashier.Cashier.SaveData;
import static cn.ytian.scashier.YLib.inFile;

public class Data {
    public static String DTDat = "dat";
    public static String DTMysql = "mysql";

    public static void writeObjectToFile(Object obj,String path) {
        inFile(path);
        File file = new File(path);
        FileOutputStream out;
        try {
            out = new FileOutputStream(file);
            ObjectOutputStream objOut = new ObjectOutputStream(out);
            objOut.writeObject(obj);
            objOut.flush();
            objOut.close();
           // System.out.println("write object success!");
        } catch (IOException e) {
            //System.out.println("write object failed");
            e.printStackTrace();
        }
    }

    public static Object readObjectFromFile(String path) {
        Object temp = null;
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        FileInputStream in;
        try {
            in = new FileInputStream(file);
            ObjectInputStream objIn = new ObjectInputStream(in);
            temp = objIn.readObject();
            objIn.close();
          //  System.out.println("read object success!");
        } catch (IOException | ClassNotFoundException e) {
           // System.out.println("read object failed");
            e.printStackTrace();
        }
        return temp;
    }

    public static void SaveOrder(Order order){
        if (Config.DataType.equals(Data.DTDat)){
            SaveData("OrderList");
        }

        if (Config.DataType.equals(Data.DTMysql)){
            YMysql.InOrder(order);
        }
    }
}
